
package com.limoilux.cobolux.core.builder;

import java.io.IOException;
import java.util.ListIterator;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;

import com.limoilux.cobolux.core.Cobolux;
import com.limoilux.cobolux.core.ProblemMarker;

public class CoboluxBuilder extends IncrementalProjectBuilder
{
	public static final String BUILDER_ID = "com.limoilux.cobolux.builder";

	private static final String MARKER_TYPE = "com.limoilux.cobolux.cobolProblem";

	@Override
	protected IProject[] build(int kind, @SuppressWarnings("rawtypes") Map args, IProgressMonitor monitor)
			throws CoreException
	{

		this.fullBuild(monitor);

		return null;
	}

	protected void fullBuild(final IProgressMonitor monitor) throws CoreException
	{
		IProject p = this.getProject();

		p.accept(new ResVisitor());

		p.refreshLocal(IProject.DEPTH_INFINITE, null);
	}

	private void comp(IResource resource)
	{
		// MessageConsole wb = new MessageConsole("Cobol", null, null);
		ListIterator<ProblemMarker> ite = null;
		ProblemMarker marker = null;
		IPath iPath = null;
		OpenCobolCompiler compiler = null;
		String name = null;
		String output = null;
		String source = null;

		iPath = resource.getLocation();
		source = iPath.toOSString();

		name = resource.getName();

		if (name.endsWith("." + Cobolux.SRC_EXTENSION))
		{
			System.out.println("CobolBuilder: start");

			compiler = new OpenCobolCompiler(Cobolux.DEF_COMPILER_PATH);

			compiler.setBinaryExtension(Cobolux.BIN_EXTENSION);

			try
			{
				System.out.println("CobolBuilder: Compiling " + source);
				compiler.compile(source);

				output = compiler.getErrorOutput();

				resource.deleteMarkers(null, true, IResource.DEPTH_INFINITE);

				ite = compiler.getMarkers();

				while (ite.hasNext())
				{
					marker = ite.next();
					this.addMarker(resource, marker);
				}
			}
			catch (CoreException e)
			{
				e.printStackTrace();
			}

			System.out.println(output);

		}
	}

	// TODO implementer un launcher
	public void launch(String s)
	{
		try
		{
			String cmd = "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal ";

			Runtime.getRuntime().exec(cmd + s);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void addMarker(IResource resource, ProblemMarker cm) throws CoreException
	{
		IMarker marker = null;
		int lineNum = 0;

		marker = resource.createMarker(CoboluxBuilder.MARKER_TYPE);

		marker.setAttribute(IMarker.MESSAGE, cm.getMessage());
		marker.setAttribute(IMarker.SEVERITY, cm.getSeverity());

		lineNum = cm.getLineNumber();

		if (lineNum > 0)
		{
			marker.setAttribute(IMarker.LINE_NUMBER, lineNum);
		}

	}

	class ResVisitor implements IResourceVisitor
	{
		@Override
		public boolean visit(IResource resource)
		{
			CoboluxBuilder.this.comp(resource);
			return true;
		}
	}
}
